﻿using Dapper;
using System;
using BCEntity.TMS.VehicleSuiteBusinessTemplate;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCCommon.Extensions;
using BCCommon.TMS.Transport;

namespace BCData.TMS.VehicleSuiteBusinessTemplate
{
	/// <summary>
	/// 车辆套餐业务模板
	/// </summary>
	public class VehicleSuiteBusinessTemplateData : IVehicleSuiteBusinessTemplateData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM vehiclesuitebusinesstemplate";
		private const string QUERY = @"SELECT A.VehicleSuiteBusinessTemplateId,A.CreateAdminName,A.CreateTime,A.UpdateTime,A.EditAdminId,A.EditAdminName,A.VehicleTransportBusinessConfigId,
											A.SuiteBusinessType,A.SuiteBusinessName,A.SuiteSlogan,A.BusinessDescriptionTitle,A.BusinessDescription,A.Enabled,A.CreateAdminId,B.TransportBusiness
										FROM vehiclesuitebusinesstemplate A 
										INNER JOIN VehicleTransportBusinessConfig B ON A.VehicleTransportBusinessConfigId=B.VehicleTransportBusinessConfigId";
		public VehicleSuiteBusinessTemplateData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查车辆套餐业务模板是否存在
		public async Task<bool> Exists(int vehicleSuiteBusinessTemplateId)
		{
			string sql = string.Format(@"{0} WHERE VehicleSuiteBusinessTemplateId=?VehicleSuiteBusinessTemplateId LIMIT 1", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleSuiteBusinessTemplateId", vehicleSuiteBusinessTemplateId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		public async Task<bool> Exists(int vehicleTransportBusinessConfigId, VehicleSuiteBusinessType suiteBusinessType)
		{
			string sql = string.Format(@"{0} WHERE VehicleTransportBusinessConfigId=?VehicleTransportBusinessConfigId AND SuiteBusinessType=?SuiteBusinessType LIMIT 1", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleTransportBusinessConfigId", vehicleTransportBusinessConfigId, DbType.Int32);
			parameters.Add("?SuiteBusinessType", suiteBusinessType.ToString(), DbType.AnsiString);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加车辆套餐业务模板
		public async Task<VehicleSuiteBusinessTemplateEntity> Add(VehicleSuiteBusinessTemplateEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("vehiclesuitebusinesstemplate", new string[] { "CreateAdminName", "VehicleTransportBusinessConfigId",
				"SuiteBusinessType", "SuiteBusinessName", "SuiteSlogan", "BusinessDescriptionTitle", "BusinessDescription",  "CreateAdminId" });
			sql = string.Format("{0} {1} WHERE A.VehicleSuiteBusinessTemplateId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleTransportBusinessConfigId", entity.VehicleTransportBusinessConfigId, DbType.Int32, size: 11);
			parameters.Add("?SuiteBusinessType", entity.SuiteBusinessType.ToString(), DbType.AnsiString);
			parameters.Add("?SuiteBusinessName", entity.SuiteBusinessType.GetDescriptionString(), DbType.AnsiString, size: 50);
			parameters.Add("?SuiteSlogan", entity.SuiteSlogan?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?BusinessDescriptionTitle", entity.BusinessDescriptionTitle?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?BusinessDescription", entity.BusinessDescription?.Trim(), DbType.String);
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleSuiteBusinessTemplateEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改车辆套餐业务模板
		public async Task<VehicleSuiteBusinessTemplateEntity> Update(VehicleSuiteBusinessTemplateEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("vehiclesuitebusinesstemplate", new string[] { "EditAdminId", "EditAdminName", "VehicleTransportBusinessConfigId", "SuiteBusinessType",
				"SuiteBusinessName", "SuiteSlogan", "BusinessDescriptionTitle", "BusinessDescription" }, new string[] { "VehicleSuiteBusinessTemplateId" });
			sql = string.Format("{0} {1} WHERE A.VehicleSuiteBusinessTemplateId=?VehicleSuiteBusinessTemplateId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleSuiteBusinessTemplateId", entity.VehicleSuiteBusinessTemplateId, DbType.Int32, size: 11);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleTransportBusinessConfigId", entity.VehicleTransportBusinessConfigId, DbType.Int32, size: 11);
			parameters.Add("?SuiteBusinessType", entity.SuiteBusinessType.ToString(), DbType.AnsiString);
			parameters.Add("?SuiteBusinessName", entity.SuiteBusinessType.GetDescriptionString(), DbType.AnsiString, size: 50);
			parameters.Add("?SuiteSlogan", entity.SuiteSlogan?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?BusinessDescriptionTitle", entity.BusinessDescriptionTitle?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?BusinessDescription", entity.BusinessDescription?.Trim(), DbType.String);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleSuiteBusinessTemplateEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除车辆套餐业务模板
		public async Task<bool> Delete(int vehicleSuiteBusinessTemplateId)
		{
			string sql = @"DELETE FROM vehiclesuitebusinesstemplate WHERE VehicleSuiteBusinessTemplateId=?VehicleSuiteBusinessTemplateId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleSuiteBusinessTemplateId", vehicleSuiteBusinessTemplateId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取车辆套餐业务模板
		public async Task<VehicleSuiteBusinessTemplateEntity> GetEntity(int vehicleSuiteBusinessTemplateId)
		{
			string sql = string.Format(@"{0} WHERE A.VehicleSuiteBusinessTemplateId=?VehicleSuiteBusinessTemplateId LIMIT 1", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleSuiteBusinessTemplateId", vehicleSuiteBusinessTemplateId, DbType.Int64);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleSuiteBusinessTemplateEntity>(sql, parameters);
		}
		public async Task<VehicleSuiteBusinessTemplateEntity> GetEntity(int vehicleTransportBusinessConfigId, VehicleSuiteBusinessType suiteBusinessType)
		{
			string sql = string.Format(@"{0} WHERE A.VehicleTransportBusinessConfigId=?VehicleTransportBusinessConfigId AND A.SuiteBusinessType=?SuiteBusinessType LIMIT 1", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleTransportBusinessConfigId", vehicleTransportBusinessConfigId, DbType.Int32, size: 11);
			parameters.Add("?SuiteBusinessType", suiteBusinessType.ToString(), DbType.AnsiString);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleSuiteBusinessTemplateEntity>(sql, parameters);
		}
		#endregion

		#region 获取车辆套餐业务模板列表
		public async Task<IEnumerable<VehicleSuiteBusinessTemplateEntity>> GetList()
		{
			string sql = string.Format(@"{0} WHERE A.Enabled=1", QUERY);
			return await this.databaseContext.Master.QueryAsync<VehicleSuiteBusinessTemplateEntity>(sql);
		}
		#endregion

		#region 获取车辆套餐业务模板分页
		public async Task<(IEnumerable<VehicleSuiteBusinessTemplateEntity>, int)> Query(string suiteBusinessName,bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(suiteBusinessName))
            {
				wheres.Add("A.SuiteBusinessName LIKE CONCAT('%',CONCAT(?SuiteBusinessName,'%'))");
				parameters.Add("?SuiteBusinessName", suiteBusinessName, DbType.AnsiString);
			}
			if (enabled.HasValue)
			{
				wheres.Add("A.Enabled=?Enabled");
				parameters.Add("?Enabled", enabled.Value, DbType.Boolean);
			}
			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("A.CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("A.CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("A.CreateTime>=?Start AND A.CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM vehiclesuitebusinesstemplate A 
							INNER JOIN VehicleTransportBusinessConfig B ON A.VehicleTransportBusinessConfigId=B.VehicleTransportBusinessConfigId {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "A.CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.QueryMultipleAsync<VehicleSuiteBusinessTemplateEntity, int>(sql, parameters);
		}
		#endregion

		#region 启用车辆套餐业务模板
		public async Task<bool> Enabled(int vehicleSuiteBusinessTemplateId, long adminId, string fullname)
		{
			string sql = @"UPDATE vehiclesuitebusinesstemplate SET Enabled=1,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName WHERE VehicleSuiteBusinessTemplateId=?VehicleSuiteBusinessTemplateId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleSuiteBusinessTemplateId", vehicleSuiteBusinessTemplateId, DbType.Int32);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 禁用车辆套餐业务模板
		public async Task<bool> Disabled(int vehicleSuiteBusinessTemplateId, long adminId, string fullname)
		{
			string sql = @"UPDATE vehiclesuitebusinesstemplate SET Enabled=0,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName WHERE VehicleSuiteBusinessTemplateId=?VehicleSuiteBusinessTemplateId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleSuiteBusinessTemplateId", vehicleSuiteBusinessTemplateId, DbType.Int32);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}

        #endregion
    }
}



